package com.youlu.campus.web.cms.service.impl;

import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.VO.res.BannerResponse;
import com.youlu.campus.service.base.BaseService;
import com.youlu.campus.web.cms.dto.BannerConfigRequest;
import com.youlu.campus.web.cms.dto.BannerConfigResponse;
import com.youlu.campus.web.cms.entity.BannerConfig;
import com.youlu.campus.web.cms.service.BannerConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BannerConfigServiceImpl extends BaseService implements BannerConfigService {

    @Override
    public Page<BannerConfigResponse> list(BannerConfigRequest request) {
        if (Objects.isNull(request)) {
            log.error(":>>> 获取列表参数错误,request参数为空");
            throw new BusinessException("Banner请求参数错误");
        }
        Query query = new Query();
        query.addCriteria(Criteria.where("status").is(Boolean.TRUE));
        if (Objects.nonNull(request.getLocation())) {
            query.addCriteria(Criteria.where("location").is(request.getLocation()));
        }
        if(StringUtils.isNotBlank(request.getPlatform())){
            query.addCriteria(Criteria.where("platform").is(request.getPlatform()));
        }
        if(StringUtils.isNotBlank(request.getAppId())){
            query.addCriteria(Criteria.where("appId").is(request.getAppId()));
        }
        if(StringUtils.isNotBlank(request.getType())){
            query.addCriteria(Criteria.where("type").is(request.getType()));
        }
        long count = mongoTemplate.count(query, BannerConfig.class);
        PageRequest pageRequest = PageRequest.of(request.getPage() - 1, request.getPageSize());
//        query.with(new Sort(Sort.Direction.DESC, "priority"));
        Sort sort=Sort.by(Sort.Direction.DESC, "priority");
        query.with(sort);
//        query.with(new Sort(Sort.Direction.DESC, "createdTime"));
        Sort sortC=Sort.by(Sort.Direction.DESC, "createdTime");
        query.with(sortC);
        List<BannerConfig> datas = mongoTemplate.find(query.with(pageRequest), BannerConfig.class);
        List<BannerConfigResponse> list = datas.stream().map(articleInfo -> {
            BannerConfigResponse response = mapperFacade.map(articleInfo, BannerConfigResponse.class);
            return response;
        }).collect(Collectors.toList());
        return PageableExecutionUtils.getPage(list, pageRequest, () -> count);
    }

    @Override
    public List<BannerConfig> findByLocations(List<String> locationList) {
        Query query = new Query().addCriteria(Criteria.where("location").in(locationList));
        return mongoTemplate.find(query, BannerConfig.class);
    }

    @Override
    public BannerResponse findByLocation(String location) {
        Query query = new Query().addCriteria(Criteria.where("location").is(location));
        query.with(Sort.by(Sort.Direction.DESC,"id"));
        BannerConfig bannerConfig = this.mongoTemplate.findOne(query, BannerConfig.class);
        if(Objects.isNull(bannerConfig)){
            return null;
        }
        BannerResponse banner = new BannerResponse();
        banner.setTitle(bannerConfig.getTitle());
        banner.setImageUrl(bannerConfig.getImageUrl());
        banner.setInnerLinks(bannerConfig.getInnerLinks());
        banner.setOuterLinks(bannerConfig.getOuterLinks());
        banner.setLinkerType(bannerConfig.getLinkerType());
        return banner;
    }
}
